import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { UserService } from '@/user/user.service';
import { LoginUserDato } from './dto/login.dto';
@Injectable()
export class AuthService {
    constructor(
        private readonly userService: UserService,
        private readonly jwtService: JwtService
    ) {
    }

    async signIn(body: LoginUserDato) {
        const { username, password } = body;
        const res = await this.userService.findUserByName(username)
        if (res === null || res.password !== password) {
            throw new UnauthorizedException('信息认证失败');
        }
        const payload = { username: body.username, id: body.id }
        return {
            status: 200,
            msg: "操作成功",
            data: {
                menu: [],
                userInfo: {},
            },
            access_token: await this.jwtService.signAsync(payload)
        }
    }

    async signOut(token: string) {
        return await this.jwtService.verifyAsync(token);
    }
}
